package com.sc.nft.admin.controller;




import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.admin.request.AddBoxPoolRequest;
import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.BoxTakePool;
import com.sc.nft.entity.DigitalCollectionProductInfo;
import com.sc.nft.entity.dto.AddWhiteDTO;
import com.sc.nft.entity.dto.WhiteListDTO;
import com.sc.nft.entity.vo.WhiteListVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.service.WorkServiceWhiteService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 城市服务中心和社区白名单考核(WorkServiceWhite)表控制层
 *
 * @author 王伟成
 * @since 2024-04-08 17:00:21
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/workServiceWhite")
@Api(tags = "社区城市服务中心白名单")
public class WorkServiceWhiteController {
    /**
     * 服务对象
     */
    private final WorkServiceWhiteService workServiceWhiteService;

    private final Redisson redisson;



    @SneakyThrows
    @PostMapping(value = "/addWhite", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加白名单", notes = "添加白名单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addWhite(@Valid @RequestBody AddWhiteDTO addWhiteDTO) {
        String key= StrUtil.format(RedisKeyEnum.CITY_COMMUNITY_WHITE.getKey(),addWhiteDTO.getUserId());
        RLock lock = redisson.getLock(key);
        try {
            boolean b = lock.tryLock(30, TimeUnit.SECONDS);
            if (b) {
                workServiceWhiteService.addWhite(addWhiteDTO);
            }
        } catch (Exception e) {
            log.error("添加白名单失败",e);
            return Result.fail(e.getMessage());
        }finally {
            if (ObjectUtil.isNotNull(lock) && lock.isLocked()) {
                lock.unlock();
            }
        }
        return Result.success();
    }


    @PostMapping(value = "/removeWhite", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "移除白名单", notes = "移除白名单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addWhite(@Valid @RequestBody IdRequest idRequest) {
        workServiceWhiteService.removeWhite(idRequest.getId());
        return Result.success();
    }

    @PostMapping(value = "/whiteList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "白名单列表", notes = "白名单列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<WhiteListVO> whiteList(@RequestBody WhiteListDTO whiteListDTO) {
        return PageResult.success(workServiceWhiteService.whiteList(whiteListDTO));
    }


    @PostMapping(value = "/screenCondition", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "筛选条件", notes = "筛选条件", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<Map> screenCondition() {
        return PageResult.success(workServiceWhiteService.screenCondition());
    }
}
